/* Copyright 2012 Dustin Mitchell <dmmitche [at] gmail [dot] com>
 *
 * Licensed under the GPLv3.  See included LICENSE.txt for full details.
 */

#ifndef __HASH__
#define __HASH__

#include <sys/types.h>

#include "list.h"
#include "iterator.h"

#ifndef HASH_INITIAL_SIZE
# define HASH_INITIAL_SIZE 16
#endif

typedef int (*hash_func)(const void *key, size_t key_size, size_t max);
typedef int (*key_cmp_func)(const void *k1, size_t k1_size,
                            const void *k2, size_t k2_size);

struct hash_pair
{
  void *key;
  size_t key_size;
  void *data;
};

struct hash
{
  struct list **data;
  size_t data_size;
  size_t num_entries;
  hash_func hfunc;
  key_cmp_func cmp_func;
};

struct hash_iter
{
  struct iter base;
  struct list_iter cur_item;
  struct list_iter end;
  int cur_hash;
  struct hash *hash;
};

int hash_create (struct hash *hash, hash_func hfunc, key_cmp_func cmp_func);

void hash_destroy (struct hash *hash);

void * hash_get (struct hash *hash, const void *key, size_t key_size);

int hash_exists (struct hash *hash, const void *key, size_t key_size);

int hash_insert (struct hash *hash, const void *key,
                 size_t key_size, void *data);

int hash_remove (struct hash *hash, const void *key, size_t key_size);

int hash_begin (struct hash *hash, struct hash_iter *iter);

int hash_end (struct hash *hash, struct hash_iter *iter);

#endif /* __HASH__ */
